package com.jk.utils;

import com.jk.bean.Wb;
import com.jk.service.ZGService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
public class Scheduler {

    protected static Logger logger = LoggerFactory.getLogger(Scheduler.class);
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("  YY:MM:dd:HH:mm:ss");

    @Resource
    ZGService zgService;


    //每隔2秒执行一次
    @Scheduled(fixedRate = 1000)
    public void testTasks() {
        String format = dateFormat.format(new Date());
     //   System.out.println("定时任务执行时间：" + format );
        String []list={"b_keji","b_caijing","b_tiyu","xzh_military","xzh_parenting","xzh_constellation","h_car","h_house","h_cate"};
        //定时发布文章
        for (String tablename : list) {
            Wb wb3 = new Wb();
            wb3.setTablename(tablename);
            List<Wb> wb=zgService.getAllTask(wb3);
            if(wb!=null){
                for (Wb wb1 : wb) {
                    Date fabuTime = wb1.getReleaseTime();
                    String format1 = dateFormat.format(fabuTime);
                    if (format.equals(format1)) {
                        Wb wb2 = new Wb();
                        wb2.setTablename(tablename);
                        wb2.setId(wb1.getId());
                        zgService.updateTask(wb2);
                        System.out.println(wb1.getBzname() + "发布成功");
                    }
                }
            }
        }
    }
}
